package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.*;
import com.vf.admin.client.service.*;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppCashWithdrawalServiceImpl  implements AppCashWithdrawalService {

    @Autowired
    AppUserDao appUserDao;
    @Autowired
    AppCashWithdrawalDao appCashWithdrawalDao;
    @Autowired
    AppSetupDao appSetupDao;
    @Autowired
    AppSetupService appSetupService;
    @Autowired
    AppCashWithdrawalMoneyDao appCashWithdrawalMoneyDao;
    @Autowired
    AppUserCollectionService userCollectionService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    SystemAlertService systemAlertService;
    @Autowired
    WithdrawFeeLevelDao withdrawFeeLevelDao;

    @Override
    public List<AppCashWithdrawal> page(Map<String, Object> paraMap, SplitPageBean splitPage) {
        return appCashWithdrawalDao.page(paraMap, splitPage);
    }

    @Override
    public List<AppCashWithdrawal> findList(Integer userId, SplitPageBean splitPage) {
        return appCashWithdrawalDao.findList(userId, splitPage);
    }

    @Override
    public void save(AppCashWithdrawal haoYou) {
        appCashWithdrawalDao.save(haoYou);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result withdraw(Integer userId, Integer id, Integer mode) {
        Map<String, Object> resultMap = new HashMap<>();
        AppCashWithdrawalMoney withdrawalMoney = appCashWithdrawalMoneyDao.get(id);
        if(withdrawalMoney != null) {
            AppUserCollection collection = userCollectionService.findByUserId(userId);
            if(collection == null) {
                return Result.errorJSON("未绑定收款账户");
            }
            if(mode == 1) {
                if(StringUtils.isBlank(collection.getBankcard()) && StringUtils.isBlank(collection.getBankcardname())) {
                    return Result.errorJSON("未绑定收款账户");
                }
            } else if (mode == 2) {
                if(StringUtils.isBlank(collection.getAliphonenum()) && StringUtils.isBlank(collection.getAliname())) {
                    return Result.errorJSON("未绑定收款账户");
                }
            }
            if(!appUserService.isEnoughGold(userId, withdrawalMoney.getGoldCoin())) {
                return Result.errorJSON("金币余额不足");
            }
            //扣金币
            appUserService.subJinbi(userId, withdrawalMoney.getGoldCoin(), "金币提现支出");
            //已申请提现金额
            BigDecimal withdrawedMoney = appCashWithdrawalDao.withdrawMoneySumByUserId(userId);
            //此阶段对应的手续费
            BigDecimal ratio = withdrawFeeLevelDao.get(withdrawedMoney);
            //计算手续费
            BigDecimal fee = withdrawalMoney.getMoney().multiply(ratio).setScale(2, BigDecimal.ROUND_HALF_UP);
            //提现记录
            AppUser user = appUserService.get(userId);
            AppCashWithdrawal cashWithdrawal = new AppCashWithdrawal();
            cashWithdrawal.setCreatetime(new Date());
            cashWithdrawal.setPhone(user.getLoginname());
            cashWithdrawal.setNick(user.getNick());
            cashWithdrawal.setPic(user.getPic());
            cashWithdrawal.setUserid(user.getId());
            cashWithdrawal.setMoney(withdrawalMoney.getMoney().subtract(fee));
            cashWithdrawal.setFee(fee);
            cashWithdrawal.setGoldCoin(withdrawalMoney.getGoldCoin());
            cashWithdrawal.setMode(mode);
            if(mode == 1) {
                if(StringUtils.isBlank(collection.getBankcard()) && StringUtils.isBlank(collection.getBankcardname())) {
                    return Result.errorJSON("未绑定收款账户");
                }
                cashWithdrawal.setBankcard(collection.getBankcard());
                cashWithdrawal.setBankcardname(collection.getBankcardname());
            } else if (mode == 2) {
                if(StringUtils.isBlank(collection.getAliphonenum()) && StringUtils.isBlank(collection.getAliname())) {
                    return Result.errorJSON("未绑定收款账户");
                }
                cashWithdrawal.setAliphonenum(collection.getAliphonenum());
                cashWithdrawal.setAliname(collection.getAliname());
            }
            cashWithdrawal.setStatus(0);
            appCashWithdrawalDao.save(cashWithdrawal);

            AppUser appUser = appUserService.get(userId);
            systemAlertService.withdrawApply(appUser.getTengxuncode());

            resultMap.put("goldCoin", withdrawalMoney.getGoldCoin());
            resultMap.put("fee", fee);
            resultMap.put("money", withdrawalMoney.getMoney().subtract(fee));
        }
        return Result.successJSON(resultMap);
    }
}
